import datetime


def flatten_view(view, rule):
    flat = {}
    for k, v in rule.items():
        try:
            if type(v) is str:
                flat[k] = view[v]
            elif type(v) is list:
                target = view
                for sub_v in v:
                    target = target[sub_v]
                flat[k] = target
        except KeyError:
            pass
    return flat


def has_permissions(required_permissions, empowered_permissions):
    """ 所需的权限是否全部被授权 """
    return set(required_permissions).issubset(set(empowered_permissions))


def has_one_of_permissions(required_permissions, empowered_permissions):
    """ 所需权限是否至少有一个被授权 """
    return len(set(required_permissions).intersection(set(empowered_permissions))) > 0


def cn_datetime():
    return datetime.datetime.now(tz=datetime.timezone(datetime.timedelta(hours=8)))
